

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.ExportException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Main extends JFrame{
	private static final long serialVersionUID = 1L;
	
	private ArrayList<Course> courses;
	private ArrayList<Exam> exams;
	
	private LoginClass login;
	private Registry reg;
	private Login stub;
	private JButton btStart;
	
	public Main() {
		courses = new ArrayList<Course>();
		exams = new ArrayList<Exam>();
		//String codebase = "file:/D:/ThiGPLXServer/bin/";
		//System.setProperty("java.rmi.server.codebase", codebase);
		try {
			reg = LocateRegistry.createRegistry(27000);
		} catch (ExportException ex) {
			System.out.println("Port da duoc su dung");
			System.exit(-1);
			
		} catch (RemoteException e) {
			e.printStackTrace();
		} 
		
		//tao cau tra loi
		LinkedHashMap<String,String> answer = new LinkedHashMap<String, String>();
		answer.put("a001", "dung");
		answer.put("a002", "sai");
		answer.put("a003", "hoi dung");
		answer.put("a004", "hoi sai");
		
		//tao ds cau hoi
		ArrayList<Question> questions = new ArrayList<Question>();
		questions.add(new Question("temp", "Ban muon lam gi?", answer));
		questions.add(new Question("temp2", "Tinh hinh la co gi hay ho?", answer));
		
		//tao bai thi
		Exam exam = new Exam("Bai thi tot nghiep", questions, 600);
		exams.add(exam);
		
		//tao khoa hoc
		Course temp = new Course("Khoa hoc day nay", "16/11", "14:00");
		
		//tao ds thi sinh
		ArrayList<Student> stus = new ArrayList<Student>();
		stus.add( new Student(temp, "10002", "Tran dat", "20/10/1993", "zxc", "zxc"));
		stus.add( new Student(temp, "10003", "Duyet", "10/14/1993", "vv", "vv"));
		stus.add( new Student(temp, "10007", "tu", "20/10/1992", "vtv", "vtv"));
		
		temp.setLengthOfExam(1000);
		temp.setStudents(stus);
		
		courses.add(temp);
		
		//tao khoa hoc
		btStart = new JButton("Bat dau!");
		btStart.addActionListener(new MainActionListener());
		
		
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		panel.add(btStart);
		add(panel);
		
		setSize(new Dimension(500,200));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	
	public static void main(String[] args) {
		
		Main e = new Main();
		e.setVisible(true);
	}
	
	class MainActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(btStart.getText().equals("Bat dau!")){
				if(startLoginForCourses(courses, exams.get(0))){
					btStart.setText("Dung");
				}
			}else{
				if(reg == null) return;
				try {
					reg.unbind("Login");
					UnicastRemoteObject.unexportObject(login, true);
					btStart.setText("Bat dau!");
					System.out.println("Dung thanh cong!");
				} catch (RemoteException | NotBoundException e1) {
					e1.printStackTrace();
				}
			}
			
		}
	}
	
	public boolean startLoginForCourses(ArrayList<Course> course, Exam exam){
		try {
			if(login == null){
				login = new LoginClass();
			}

			for(Course e : course){
				login.addNewCourse(e);
				for(Student stu : e.getStudentList()){
					stu.setExam(exam);
				}
			}
			
			stub = (Login)UnicastRemoteObject.exportObject(login,0);
			reg.rebind("Login", stub);

			System.out.println("Khoi tao thanh cong!");
			return true;
		} catch (RemoteException ex) {
			ex.printStackTrace();
		}
		return false;
	}

}
